package com.xinxin.intelligenceexam.base;

import android.app.Application;
import android.content.Context;

import com.facebook.stetho.Stetho;
import com.orhanobut.logger.Logger;
import com.orm.SugarContext;
import com.xinxin.intelligenceexam.domain.Reply;
import com.xinxin.intelligenceexam.utils.ToastUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * User: Tianxin
 * Date: 2016-11-06
 * Time: 23:00
 */

public class MyApplication extends Application {
    public static int DEFAULT_EXAM_QUESTION_NUM = 25;
    public static MyApplication instance;
    private final String APPLICATION_TAG = "richy";
    public Context mContext;
    Map<Integer, Reply> mReplies;


    public static MyApplication getInstance() {
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        mContext = getApplicationContext();
        initLibrary();
        if (Constant.DEBUG) {
            initDebugLibrary();
        }
    }

    private void initLibrary() {
        SugarContext.init(this);
        ToastUtils.register(this);
//        CrashHandler crashHandler = CrashHandler.getInstance();
//        crashHandler.init(this, true);

    }

    private void initDebugLibrary() {
        Logger
                .init(APPLICATION_TAG)                 // default PRETTYLOGGER or use just init()
                .methodCount(2)                 // default 2
                //.hideThreadInfo()               // default shown
                //.logLevel(LogLevel.NONE)        // default LogLevel.FULL,Note: Use LogLevel.NONE for the release versions.
                .methodOffset(2);              // default 0
        //.logAdapter(new AndroidLogAdapter()); //default AndroidLogAdapter
        Stetho.initialize(
                Stetho.newInitializerBuilder(this)
                        .enableDumpapp(Stetho.defaultDumperPluginsProvider(this))
                        .enableWebKitInspector(Stetho.defaultInspectorModulesProvider(this))
                        .build());
        //LeakCanary.install(this);
    }

    public Map<Integer, Reply> getReplies() {
        return mReplies;
    }


    public void addReply(int index, Reply reply) {
        if (mReplies == null) {
            mReplies = new HashMap<Integer, Reply>();
        }
        //直接插入，不检验了
        mReplies.put(index, reply);
    }

    /**
     * 是否包含某个序号的回答
     *
     * @param index
     * @return 包含返回true，不包含是false
     */
    public boolean containReply(int index) {
        return mReplies.containsKey(index);
    }
}
